package com.netease.nim.demo.base;

import java.util.ArrayList;
import java.util.List;

import logic.constants.DefaultConsts;
import logic.util.AppUtils;
import logic.util.Util;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.config.preference.Preferences;
import com.netease.nim.demo.contact.cache.ContactDataCache;
import com.netease.nim.demo.contact.cache.TeamDataCache;
import com.netease.nim.demo.contact.cache.TeamMemberInfoCache;
import com.netease.nim.demo.contact.protocol.ContactHttpClient;
import com.netease.nim.demo.database.DatabaseManager;
import com.netease.nim.demo.util.LogUtil;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.auth.AuthService;
import com.shwread.android.ui.dialog.SingleSignOnDialog;


public class YXBaseActivity
        extends
        FragmentActivity
{
	public static List<Activity> activityList = new ArrayList<Activity>();
	/** 接收用户异常登录的广播 */
	private LoginReceiver        receiver;
	private SingleSignOnDialog   dialog;


	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		activityList.add(this);
		receiver = new LoginReceiver();
		registerReceiver(receiver, new IntentFilter(DefaultConsts.EXCEPTION_LOGIN_STRING));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (receiver != null)
		{
			unregisterReceiver(receiver);
			receiver = null;
		}
	}
	
	public static void exit() {
		for (Activity activity : activityList)
		{
			LogUtil.e(DefaultConsts.TAG, "YXBaseActivity -->" + activity.getClass().getSimpleName() + " -- finish!!");
			activity.finish();
		}
		activityList.clear();
		
		
//		onLogout();
	}
	private static void onLogout() {
		TeamMemberInfoCache.destroy();
		TeamDataCache.getInstance().clearAll();
		ContactDataCache.getInstance().clearAll();
		NimCache.clear();
		NIMClient.getService(AuthService.class).logout();
		Preferences.saveUserToken("");
        DatabaseManager.getInstance().close();
        ContactDataCache.getInstance().clearBuddyCache();
        TeamDataCache.getInstance().clearTeamCache();
        ContactHttpClient.getInstance().resetToken();
    }
	
	private class LoginReceiver
	        extends
	        BroadcastReceiver
	{
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			final String json = arg1.getStringExtra("json");
			final String packageName = arg1.getStringExtra("packageName");
			if (Util.isNotEmpty(packageName) && packageName.equals(AppUtils.getPackageName(arg0)) && Util.isNotEmpty(json))
			{
				if (null == dialog) dialog = new SingleSignOnDialog(arg0);
				dialog.setJson(arg1.getStringExtra("json"));
				dialog.setCancelable(false);
				if (!dialog.isShowing()) dialog.show();
			}
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
	}
}
